<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Kegiatan_model extends MY_Model {

    function __construct() {
        parent::__construct("activity", "id", Array(
            "content_id" => "Content_model"
        ));
        $this->delAttribute = "is_deleted";
    }

    public function create($title, $text, $thumbId, $bannerId, $location, $startDate, $endDate, $tags = Array()) {
        $this->load->model("User_model");
        $curUserId = $this->curUserId();
        $roles = $this->User_model->fetchRoles($curUserId);
        $isAdmin = in_array("kegiatan_auditor", $roles);
        $contentId = $this->Content_model->create($title, $text, $thumbId, $bannerId, $tags);
        return parent::create(Array(
                    "content_id" => $contentId,
                    "location" => $location,
                    "start_date" => $startDate,
                    "end_date" => $endDate,
                    "is_approved" => $isAdmin
        ));
    }

    protected function read($where = Array(), $fetchForeign = false, $depth = 1, $page = -1, $limit = 10) {
        $results = parent::read($where, $fetchForeign, $depth, $page, $limit);

        foreach ($results as $result) {
            $stdate = explode(" ", $result->start_date);
            $endate = explode(" ", $result->end_date);
            $result->start_date = $stdate[0];
            $result->end_date = $endate[0];
            if (is_object($result->content_id)) {
                $shortTrimmed = preg_replace("/(<([^>]*)>)/i", "", $result->content_id->text);
                $shortTrimmed = substr($shortTrimmed, 0, 220);
                $shortTrimmed .= " <a href='" . site_url("kegiatan/detail/" . $result->id) . "'>Lebih lanjut...</a>";
                $result->content_id->short = $shortTrimmed;
            }
            // try extract loc obj
            $loc_obj = null;
            $loc_type = "ETC";
            $locArr = explode(" ", $result->location, 2);
            if (count($locArr) > 1) {
                $firstSplit = $locArr[0];
                $secondSplit = $locArr[1];
                switch ($firstSplit) {
                    case "Region" :
                        $this->load->model("Region_model");
                        $kodeArr = explode(" ", $secondSplit, 2);
                        $regObj = $this->Region_model->findById($kodeArr[0]);
                        if ($regObj != null) {
                            $loc_type = "REG";
                            $loc_obj = $regObj;
                        }
                        break;
                    case "Provinsi" :
                        $this->load->model("Provinsi_model");
                        $provAll = $this->Provinsi_model->findAll(Array("display_name" => $secondSplit));
                        if (count($provAll) > 0) {
                            $loc_type = "PROV";
                            $loc_obj = $provAll[0];
                        }
                        break;
                    case "Kab." :
                        $this->load->model("Kabupaten_model");
                        $kodeArr = explode(",", $secondSplit, 2);
                        $kabAll = $this->Kabupaten_model->findAll(Array("display_name" => $kodeArr[0]));
                        if (count($kabAll) > 0) {
                            $loc_type = "KAB";
                            $loc_obj = $kabAll[0];
                        }
                        break;
                    case "Kec." :
                        $this->load->model("Kecamatan_model");
                        $kodeArr = explode(",", $secondSplit, 2);
                        $kecAll = $this->Kecamatan_model->findAll(Array("display_name" => $kodeArr[0]));
                        if (count($kecAll) > 0) {
                            $loc_type = "KEC";
                            $loc_obj = $kecAll[0];
                        }
                        break;
                }
            }
            $result->loc_obj = $loc_obj;
            $result->loc_type = $loc_type;
        }

        return $results;
    }

    public function searchByPoster($idPoster, $fetchForeign = false, $depth = 1) {
        $query = $this->db->query("SELECT activity.id FROM activity, content WHERE activity.content_id = content.id AND activity.is_deleted = 0 AND content.poster_id = " . $idPoster . " ORDER BY activity.is_approved ASC, content.posted DESC LIMIT 0, 10");
        $resArr = Array();

        foreach ($query->result() as $res) {
            array_push($resArr, $this->findById($res->id, $fetchForeign, $depth));
        }

        return $resArr;
    }

    public function deleteById($id) {
        $this->load->model("Beritakegiatan_model");
        if (parent::deleteById($id)) {
            if ($this->Beritakegiatan_model->deleteAll(Array("activity_id" => $id))) {
                echo 1;
            }
        }
    }

    public function makeHL($id) {
        $kegiatan = $this->Kegiatan_model->findById($id);
        if ($kegiatan != NULL) {
            if ($kegiatan->is_approved) {
                $this->db->from("hl_activity")->where("activity_id", $kegiatan->id);
                $query = $this->db->get();
                $hl = $query->row();
                if ($hl != NULL) {
                    echo $this->db->update("hl_activity", Array("timestamp" => date("Y-m-d H:i:s")), Array("id" => $hl->id));
                } else {
                    echo $this->db->insert("hl_activity", Array("timestamp" => date("Y-m-d H:i:s"), "activity_id" => $kegiatan->id));
                }
            }
        }

        return 0;
    }

    public function fetchHL($limit = 5) {
        $this->db->select("hl_activity.activity_id as id")->from("hl_activity, activity")->where("hl_activity.activity_id = activity.id AND activity.is_approved = 1 AND activity.is_deleted = 0")->order_by("hl_activity.timestamp", "DESC")->limit($limit, 0);
        $query = $this->db->get();
        $kegList = Array();
        foreach ($query->result() as $row) {
            $kegiatan = $this->findById($row->id, true, 2);
            if ($kegiatan != NULL) {
                array_push($kegList, $kegiatan);
            }
        }
        return $kegList;
    }

}